package org.millioncall.yueyoga.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.NumberUtils;
import org.millioncall.yueyoga.admin.dao.CardReportsDAO;
import org.millioncall.yueyoga.admin.model.CardModel;
import org.millioncall.yueyoga.admin.model.CardReports;
import org.millioncall.yueyoga.admin.model.ChargerModel;
import org.millioncall.yueyoga.admin.model.ChargerReport;
import org.millioncall.yueyoga.admin.model.HallName;
import org.millioncall.yueyoga.admin.model.MonthConsult;
import org.millioncall.yueyoga.admin.model.MonthReport;
import org.millioncall.yueyoga.admin.model.PayModel;
import org.millioncall.yueyoga.admin.service.CardReportsService;

public class CardReportsServiceImpl implements CardReportsService {
	private CardReportsDAO cardReportsDAO;

	public CardReportsDAO getCardReportsDAO() {
		return cardReportsDAO;
	}

	public void setCardReportsDAO(CardReportsDAO cardReportsDAO) {
		this.cardReportsDAO = cardReportsDAO;
	}

	@Override
	public PaginatedList<CardReports> findCardCounts(int pageNo, int pageSize,
			Map<String, Object> map) {
		return this.cardReportsDAO.findCardCounts(pageNo, pageNo, map);
	}

	@Override
	public CardModel findCardCounts(Map<String, Object> map) {
		CardModel cm = new CardModel();
		
		List<CardReports> cardReportsList = this.cardReportsDAO
				.findCardCounts(map);
		List<CardReports> crList = new ArrayList<CardReports>();
		Map<Integer, CardReports> map2 = new HashMap<Integer, CardReports>();

		for (CardReports cr : cardReportsList) {
			if (map2.containsKey(cr.getId())) {
				CardReports crt = map2.get(cr.getId());
				if (cr.getConsume().equals("新销售")) {
					crt.setNewSellCount(cr.getCount() == 0 ? 0 : cr.getCount());
					crt.setNewSellAmount(cr.getTotalAmount() == 0.0 ? 0.0 : cr
							.getTotalAmount());
					crt.setNewSellMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				} else if (cr.getConsume().equals("定金")) {
					crt.setDepCount(cr.getCount());
					crt.setDepAmount(cr.getTotalAmount() == 0.0 ? 0.0 : cr
							.getTotalAmount());
					crt.setDepMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				} else if (cr.getConsume().equals("续费")) {
					crt.setRenewCount(cr.getCount() == null ? 0 : cr.getCount());
					crt.setRenewAmount(cr.getTotalAmount());
					crt.setRenewMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				} else if (cr.getConsume().equals("退款")) {
					crt.setRefundCount(cr.getCount() == null ? 0 : cr
							.getCount());
					crt.setRefundAmount(cr.getTotalAmount() == 0.0 ? 0.0 : (-cr
							.getTotalAmount()));
					crt.setRefundMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				}
			} else {
				if (cr.getConsume().equals("新销售")) {
					cr.setNewSellCount(cr.getCount() == null ? 0 : cr
							.getCount());
					cr.setNewSellAmount(cr.getTotalAmount() == 0.0 ? 0.0 : cr
							.getTotalAmount());
					cr.setNewSellMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				} else if (cr.getConsume().equals("定金")) {
					cr.setDepCount(cr.getCount() == null ? 0 : cr.getCount());
					cr.setDepAmount(cr.getTotalAmount() == 0.0 ? 0.0 : cr
							.getTotalAmount());
					cr.setDepMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				} else if (cr.getConsume().equals("续费")) {
					cr.setRenewCount(cr.getCount() == null ? 0 : cr.getCount());
					cr.setRenewAmount(cr.getTotalAmount() == 0.0 ? 0.0 : cr
							.getTotalAmount());
					cr.setRenewMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				} else if (cr.getConsume().equals("退款")) {
					cr.setRefundCount(cr.getCount() == null ? 0 : cr.getCount());
					cr.setRefundAmount(cr.getTotalAmount() == 0.0 ? 0.0 : (-cr
							.getTotalAmount()));
					cr.setRefundMean(cr.getAvg() == 0.0 ? 0.0 : cr.getAvg());
				}
				map2.put(cr.getId(), cr);
			}
		}
		int xxsczj = 0, djczj = 0, xfczj = 0, tfczj = 0, totalczj = 0;
		double xxszj = 0.00, djzj = 0.00, xfzj = 0.00, tfzj = 0.00, totalzj = 0.00;
		Iterator<Entry<Integer, CardReports>> it = map2.entrySet().iterator();
		while (it.hasNext()) {
			CardReports c = it.next().getValue();
			int count = (c.getDepCount() == null ? 0 : c.getDepCount())
					+ (c.getNewSellCount() == null ? 0 : c.getNewSellCount())
					+ (c.getRenewCount() == null ? 0 : c.getRenewCount())
					+ (c.getRefundCount() == null ? 0 : c.getRefundCount());
			double amount = (c.getDepAmount() == 0.0 ? 0.0 : c.getDepAmount())
					+ (c.getNewSellAmount() == 0.0 ? 0.0 : c.getNewSellAmount())
					+ (c.getRenewAmount() == 0.0 ? 0.0 : c.getRenewAmount())
					+ (c.getRefundAmount() == 0.0 ? 0.0 : c.getRefundAmount());
			c.setTotal(count);
			c.setTotalAmount(amount);
			
			
			xxsczj += c.getNewSellCount();
			djczj += c.getDepCount();
			xfczj += c.getRenewCount();
			tfczj += c.getRefundCount();
			
			totalczj += c.getTotal();
			
			xxszj = NumberUtils.add(xxszj, c.getNewSellAmount());
			djzj = NumberUtils.add(djzj, c.getDepAmount());
			xfzj = NumberUtils.add(xfzj, c.getRenewAmount());
			tfzj = NumberUtils.add(tfzj, c.getRefundAmount());
			
			totalzj = NumberUtils.add(totalzj, c.getTotalAmount());
			
			crList.add(c);
		}
		
		cm.setCardReportList(crList);
		
		cm.setXxsczj(xxsczj);
		cm.setDjczj(djczj);
		cm.setXfczj(xfczj);
		cm.setTfczj(tfczj);
		
		cm.setTotalczj(totalczj);
		
		cm.setXxszj(xxszj);
		cm.setDjzj(djzj);
		cm.setXfzj(xfzj);
		cm.setTfzj(tfzj);
		
		cm.setTotalzj(totalzj);
		
		return cm;
	}

	@Override
	public HallName hallName(String hallName) {
		return this.cardReportsDAO.hallName(hallName);
	}

	@Override
	public ChargerModel findChargerModel(Map<String, Object> map) {
		ChargerModel cm = new ChargerModel();
		List<ChargerReport> newList = new ArrayList<ChargerReport>();
		List<ChargerReport> list = this.cardReportsDAO.findChargerReport(map);
		Map<String, ChargerReport> chargerMap = new HashMap<String, ChargerReport>();
		for (ChargerReport cr : list) {
			if (chargerMap.containsKey(cr.getChargerName())) {
				ChargerReport cr2 = chargerMap.get(cr.getChargerName());
				if ("新销售".equals(cr.getType()))
					cr2.setXxsAchievement(cr.getAchievement());
				else if ("商品".equals(cr.getType()))
					cr2.setSpAchievement(cr.getAchievement());
				else if ("续费".equals(cr.getType()))
					cr2.setXfAchievement(cr.getAchievement());
				else if ("充值".equals(cr.getType()))
					cr2.setCzAchievement(cr.getAchievement());
			} else {
				ChargerReport cr2 = new ChargerReport();
				cr2.setType(cr.getType());
				cr2.setOrganizationId(cr.getOrganizationId());
				cr2.setOrganizationName(cr.getOrganizationName());
				cr2.setChargerName(cr.getChargerName());
				cr2.setAchievement(cr.getAchievement());
				cr2.setXxsAchievement(cr.getXxsAchievement());
				cr2.setSpAchievement(cr.getSpAchievement());
				cr2.setCzAchievement(cr.getCzAchievement());
				cr2.setXfAchievement(cr.getXfAchievement());
				if ("新销售".equals(cr.getType()))
					cr2.setXxsAchievement(cr.getAchievement());
				else if ("商品".equals(cr.getType()))
					cr2.setSpAchievement(cr.getAchievement());
				else if ("续费".equals(cr.getType()))
					cr2.setXfAchievement(cr.getAchievement());
				else if ("充值".equals(cr.getType()))
					cr2.setCzAchievement(cr.getAchievement());
				chargerMap.put(cr2.getChargerName(),cr2);
			}
		}
		
		Double xxs = 0.00, sp = 0.00, xf = 0.00, cz = 0.00;
		Iterator<Entry<String, ChargerReport>> it = chargerMap.entrySet().iterator();
		while (it.hasNext()) {
			ChargerReport cr = it.next().getValue();
			xxs = NumberUtils.add(xxs, cr.getXxsAchievement());
			sp = NumberUtils.add(sp, cr.getSpAchievement());
			xf = NumberUtils.add(xf, cr.getXfAchievement());
			cz = NumberUtils.add(cz, cr.getCzAchievement());
			newList.add(cr);
		}
		cm.setXxszyj(xxs);
		cm.setSpzyj(sp);
		cm.setXfzyj(xf);
		cm.setCzzyj(cz);
		cm.setChargeReportList(newList);
		return cm;
	}

	@Override
	public MonthConsult findMonthReport(Map<String, Object> map) {
		List<MonthReport> monthReports = this.findMonthReports(map);
		MonthConsult mc = new MonthConsult();
		List<MonthReport> motList = new ArrayList<MonthReport>();
		Map<Integer, MonthReport> map2 = new HashMap<Integer, MonthReport>();
		MonthReport monthReport = null;
		for (MonthReport mon : monthReports) {
			if (map2.containsKey(mon.getMonths())) {
				monthReport = map2.get(mon.getMonths());
				if (("新销售").equals(mon.getTypeName())) {
					monthReport.setNewSellName(mon.getTypeName());
					monthReport.setNewSellResults(mon.getTypeResults());
				} else if (("续费").equals(mon.getTypeName())) {
					monthReport.setRenew(mon.getTypeName());
					monthReport.setRenewResults(mon.getTypeResults());
				} else if (("商品").equals(mon.getTypeName())) {
					monthReport.setGoods(mon.getTypeName());
					monthReport.setGoodsResults(mon.getTypeResults());
				}else if(("充值").equals(mon.getTypeName())){
					monthReport.setPay(mon.getTypeName());
					monthReport.setPayResults(mon.getTypeResults());
				}
			} else {
				if (("新销售").equals(mon.getTypeName())) {
					mon.setNewSellName(mon.getTypeName());
					mon.setNewSellResults(mon.getTypeResults());
				} else if (("续费").equals(mon.getTypeName())) {
					mon.setRenew(mon.getTypeName());
					mon.setRenewResults(mon.getTypeResults());
				} else if (("商品").equals(mon.getTypeName())) {
					mon.setGoods(mon.getTypeName());
					mon.setGoodsResults(mon.getTypeResults());
				}else if(("充值").equals(mon.getTypeName())){
					mon.setPay(mon.getTypeName());
					mon.setPayResults(mon.getTypeResults());
				}
				map2.put(mon.getMonths(), mon);
			}
		}
		Iterator<Entry<Integer, MonthReport>> it = map2.entrySet().iterator();
		Double xx = 0.00, xf = 0.00, sp = 0.00,cz = 0.00;
		while (it.hasNext()) {
			MonthReport m = it.next().getValue();
			xx+=m.getNewSellResults();
			xf+=m.getRenewResults();
			sp+=m.getGoodsResults();
			cz+=m.getPayResults();
			motList.add(m);
		}
		mc.setMont(motList);
		mc.setGoodstotalResults(sp);
		mc.setNewSelltotalResults(xx);
		mc.setPaytotalResults(cz);
		mc.setRenewtotalResults(xf);
		return mc;
	}


	@Override
	public List<MonthReport> findMonthReports(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.cardReportsDAO
				.findMonthReports(map);
	}

	@Override
	public List<PayModel> findrecharges(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return null;
	}
}
